﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CellphoneRepair.DAL;
using CellphoneRepair.Entities;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;

namespace CellphoneRepair.UserControls
{
    public partial class Uc_pinpai : CellphoneRepair.UserControls.Uc_list_base
    {
        public Uc_pinpai()
        {
            InitializeComponent();
        }

        private void Uc_pinpai_Load(object sender, EventArgs e)
        {
            bindingSource1.DataSource = typeof (Pinpai);

            using (PinpaiBll bll = new PinpaiBll())
            {
                bindingSource1.DataSource = bll.GetAll();
            }
            bindingSource1.AddingNew += bindingSource1_AddingNew;
        }

        private void bindingSource1_AddingNew(object sender, AddingNewEventArgs e)
        {
            Pinpai obj = new Pinpai();
            obj.Idpinpai = Commons.GenerateIdByForeTag("PP");
            e.NewObject = obj;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal override void simpleButton3_Click(object sender, EventArgs e)
        {
            base.simpleButton3_Click(sender, e);

            using (PinpaiBll bll = new PinpaiBll())
            {
                if (XtraMessageBox.Show("是否确定要删除这条记录？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    bll.Delete(bindingSource1.Current as Pinpai);

                    bindingSource1.RemoveCurrent();
                }
            }
        }

        internal override void gridView1_RowUpdated(object sender, RowObjectEventArgs e)
        {
            base.gridView1_RowUpdated(sender, e);
            using (PinpaiBll bll = new PinpaiBll())
            {
                switch (editType)
                {
                    case EnumEditType.Create:
                        bll.Insert(bindingSource1.Current as Pinpai);
                        break;
                    case EnumEditType.Edit:
                        bll.Update(bindingSource1.Current as Pinpai);
                        break;
                }
            }
        }
    }
}